package haida.pym_system.site.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import haida.pym_system.site.pojo.User;
import haida.pym_system.site.mapper.UserMapper;
import haida.pym_system.site.pojo.Vo;
import haida.pym_system.site.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 大龙
 * @since 2020-09-28
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Resource
    UserMapper userMapper;

    /**
     * 根据名字查询
     *
     * @param username
     * @return
     */
    @Override
    public User findUserName(String username) {
        QueryWrapper<User> userWrapper = new QueryWrapper<>();
        userWrapper
                .eq("username", username);

        User user = userMapper.selectOne(userWrapper);
        return user;
    }

    @Override
    public User findUserByID(int id) {
        return userMapper.selectById(id);
    }

    /**
     * 根据名字和密码登录
     *
     * @param user
     * @return
     */
    @Override
    public User login(User user) {
        QueryWrapper<User> userWrapper = new QueryWrapper<>();
        userWrapper
                .ge("username", user.getUsername())
                .ge("password", user.getPassword());
        return userMapper.selectOne(userWrapper);
    }

    /**
     * 注册用户
     *
     * @param user
     * @return
     */
    @Override
    public int registerUser(User user) {
        return userMapper.insert(user);
    }

    /**
     * 修改密码
     *
     * @param user
     * @return
     */
    @Override
    public int updateUser(User user) {
        UpdateWrapper<User> userWrapper = new UpdateWrapper<>();
        userWrapper
                .eq("username", user.getUsername())
                .set("password", user.getPassword());

        return userMapper.update(null, userWrapper);
    }

    @Override
    public List<User> getAllUser() {
        return userMapper.selectList(null);
    }

    @Override
    public Vo<User> queryUserForPage(String content, int current, int size) {
        if (content == null && content.equals("")) {
            current = 1;
        }
        System.out.println(content + current + size);

        IPage<User> userPage = new Page<>(current, size);//参数一是当前页，参数二是每页个数
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.like("username", content);
        userPage = userMapper.selectPage(userPage, wrapper);
        Vo<User> userVo = new Vo<>();
        userVo.setContent(content);
        userVo.setCurrent(current);
        userVo.setSize(size);
        userVo.setTotal(userPage.getTotal());
        userVo.setList(userPage.getRecords());
        return userVo;
    }

    @Override
    public int deleteUser(int id) {
        return userMapper.deleteById(id);
    }

    @Override
    public int setUserState(int userID,int setV) {
        UpdateWrapper<User> userWrapper = new UpdateWrapper<>();
        userWrapper.set("state", setV)
                .eq("userID", userID);
        return userMapper.update(null, userWrapper);
    }


}
